package com.icode.core.dto.house;

import com.icode.core.model.house.*;
import com.icode.core.shared.ImageUtils;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.Pattern;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: sunk
 * Date: 15-1-4
 * Time: 下午1:53
 * To change this template use File | Settings | File Templates.
 */
public class BuildingTypeDTO {
    private String guid;
    @NotEmpty(message = "户型名不能为空!")
    private String name;
    @Pattern(regexp = "^[+-]?\\d+(\\.\\d+)?$", message = "请输入正确的面积!")
    private String area;
    private String price;
    private String personNum;
    private String houseTypeAsString;
    private String houseTypeAsValue;
    private List<ForEnumDTO> houseTypes = HouseType.toDTOs();
    private String houseName;
    private String houseInfoGuid;
    private String houseBaseGuid;
    private String buildingArea;
    private String extraArea;
    private List<MultipartFile> images;
    private List<HouseInfoDTO> houseInfoDTOs;
    private List<String> imageGuids;

    public BuildingTypeDTO() {
    }

    public BuildingTypeDTO(String guid, String name, String area, String price, String buildingArea, String extraArea, String personNum, String houseTypeAsString, String houseTypeAsValue, List<String> imageGuids) {
        this.guid = guid;
        this.name = name;
        this.area = area;
        this.price = price;
        this.buildingArea = buildingArea;
        this.extraArea = extraArea;
        this.personNum = personNum;
        this.houseTypeAsString = houseTypeAsString;
        this.houseTypeAsValue = houseTypeAsValue;
        this.imageGuids = imageGuids;
    }

    public BuildingTypeDTO(String baseGuid) {
        this.houseBaseGuid = baseGuid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }

    public void setImageGuids(List<String> imageGuids) {
        this.imageGuids = imageGuids;
    }

    public List<String> getImageGuids() {

        return imageGuids;
    }

    public String getPersonNum() {
        return personNum;
    }

    public void setPersonNum(String personNum) {
        this.personNum = personNum;
    }

    public String getHouseTypeAsValue() {
        return houseTypeAsValue;
    }

    public void setHouseTypeAsValue(String houseTypeAsValue) {
        this.houseTypeAsValue = houseTypeAsValue;
    }

    public String getHouseTypeAsString() {
        return houseTypeAsString;
    }

    public void setHouseTypeAsString(String houseTypeAsString) {
        this.houseTypeAsString = houseTypeAsString;
    }

    public List<ForEnumDTO> getHouseTypes() {
        return houseTypes;
    }

    public void setHouseTypes(List<ForEnumDTO> houseTypes) {
        this.houseTypes = houseTypes;
    }

    public void setHouseInfoDTOs(List<HouseInfoDTO> houseInfoDTOs) {
        this.houseInfoDTOs = houseInfoDTOs;
    }

    public List<HouseInfoDTO> getHouseInfoDTOs() {

        return houseInfoDTOs;
    }

    public String getGuid() {
        return guid;
    }

    public void setGuid(String guid) {
        this.guid = guid;
    }

    public String getHouseName() {
        return houseName;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getPrice() {

        return price;
    }

    public void setHouseName(String houseName) {
        this.houseName = houseName;
    }

    public String getHouseInfoGuid() {
        return houseInfoGuid;
    }

    public void setHouseInfoGuid(String houseInfoGuid) {
        this.houseInfoGuid = houseInfoGuid;
    }

    public String getHouseBaseGuid() {
        return houseBaseGuid;
    }

    public void setHouseBaseGuid(String houseBaseGuid) {
        this.houseBaseGuid = houseBaseGuid;
    }

    public String getBuildingArea() {
        return buildingArea;
    }

    public void setBuildingArea(String buildingArea) {
        this.buildingArea = buildingArea;
    }

    public String getExtraArea() {
        return extraArea;
    }

    public void setExtraArea(String extraArea) {
        this.extraArea = extraArea;
    }

    public List<MultipartFile> getImages() {
        return images;
    }

    public void setImages(List<MultipartFile> images) {
        this.images = images;
    }

    public BuildingType toBuildingType(HouseBase houseBase) throws IOException {
        HouseType houseType = HouseType.valueOf(this.houseTypeAsValue);
        BuildingType buildingType = new BuildingType(this.name, this.area, this.price, houseType, this.personNum, houseBase, this.buildingArea, this.extraArea);
        List<UploadImage> uploadImages = ImageUtils.resolveUploadImages(this.images);
        List<BuildingTypeImage> buildingTypeImages = toHouseTypeImages(buildingType, uploadImages);
        buildingType.initHouseTypeImages(buildingTypeImages);
        return buildingType;
    }

    public BuildingType toBuildingTypeForUpdate(BuildingType buildingType, HouseBase houseBase) {
        HouseType houseType = HouseType.valueOf(this.houseTypeAsValue);
        buildingType.initBuildingType(this.name, this.area, this.price, houseType, this.personNum, houseBase, this.buildingArea, this.extraArea);
        if (!"".equals(this.images.get(0).getOriginalFilename())) {
            List<UploadImage> uploadImages = ImageUtils.resolveUploadImages(this.images);
            List<BuildingTypeImage> buildingTypeImages = toHouseTypeImages(buildingType, uploadImages);
            buildingType.initHouseTypeImages(buildingTypeImages);
        }
        return buildingType;
    }

    private List<BuildingTypeImage> toHouseTypeImages(BuildingType buildingType, List<UploadImage> uploadImages) {
        List<BuildingTypeImage> buildingTypeImages = new ArrayList<>();
        for (UploadImage image : uploadImages) {
            BuildingTypeImage buildingTypeImage = new BuildingTypeImage(buildingType, image);
            buildingTypeImages.add(buildingTypeImage);
        }
        return buildingTypeImages;
    }

    public static List<BuildingTypeDTO> toDTOs(List<BuildingType> buildingTypes) {
        List<BuildingTypeDTO> buildingTypeDTOs = new ArrayList<>();
        for (BuildingType buildingType : buildingTypes) {
            BuildingTypeDTO buildingTypeDTO = toDTO(buildingType);
            buildingTypeDTOs.add(buildingTypeDTO);
        }
        return buildingTypeDTOs;
    }

    public static BuildingTypeDTO toDTO(BuildingType buildingType) {
        String guid = buildingType.guid();
        String area = buildingType.area();
        String personNum = buildingType.personNum();
        String label = buildingType.houseType().getLabel();
        String value = buildingType.houseType().getValue();
        String name = buildingType.name();
        String price = buildingType.price();
        String buildingArea = buildingType.buildingArea();
        String extraArea = buildingType.extraArea();
        List<String> imageGuids = initImageGuids(buildingType.buildingTypeImages());
        return new BuildingTypeDTO(guid, name, area, price, buildingArea, extraArea, personNum, label, value, imageGuids);
    }

    private static List<String> initImageGuids(List<BuildingTypeImage> buildingTypeImages) {
        List<String> imageGuids = new ArrayList<>();
        for (BuildingTypeImage b : buildingTypeImages) {
            String guid = b.image().guid();
            imageGuids.add(guid);
        }
        return imageGuids;
    }

    private static String getHouseInfoGuid(BuildingType buildingType) {
        String houseInfoGuid = null;
        boolean isHouseUnExists = buildingType.houseInfos().isEmpty();
        if (!isHouseUnExists) {
            houseInfoGuid = buildingType.houseInfos().get(0).guid();
        }
        return houseInfoGuid;
    }

    private static String getHouseName(BuildingType buildingType) {
        String houseName = null;
        boolean isHouseUnExists = buildingType.houseInfos().isEmpty();
        if (!isHouseUnExists) {
            houseName = buildingType.houseInfos().get(0).baseInfo().houseName();
        }
        return houseName;
    }
}
